<template>
  <div id="rePwd">
    <p style="color: #8d9192">Tip:绑定了邮箱的学生可以通过邮箱+身份证验证的方式找回密码,否则请联系学校修改密码</p><br>
    <el-form  label-position="left" label-width="110px" :model="forget">
      <el-form-item label="您的名称">
        <el-input placeholder="请输入名称" v-model="forget.name"></el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="forget.type" placeholder="选择用户类型">
          <el-option label="高校" value="university"></el-option>
          <el-option label="企业" value="company"></el-option>
          <el-option label="学生" value="student"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="forget.type=='student'" label="身份证号码">
        <el-input placeholder="输入身份证号码" v-model="forget.idCard"></el-input>
      </el-form-item>
      <el-form-item label="绑定邮箱">
        <el-input style="width: 500px" placeholder="请输入绑定邮箱" v-model="forget.email"></el-input>
        <el-button style="margin-left: 10px" type="danger" :disabled="!allowSend?'disabled':false" @click="sendCapt">{{allowSend?'发送验证码':`${time}s以后可重新发送`}}</el-button>
      </el-form-item>
      <el-form-item label="输入验证码">
        <el-input placeholder="输入验证码" v-model="forget.captcha"></el-input>
      </el-form-item>
      <el-form-item v-model="forget.newPassword" label="输入新密码">
        <el-input placeholder="输入新密码" v-model="forget.newPassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.push('login')" type="danger">返回</el-button>
        <el-button @click="setNewPwd" type="success">提交</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import {getForgetCaptcha, reSetPwd} from "@/api/user";

export default {
  name: "rePwd",
  data(){
    return{
      allowSend:true,
      time:30,
      timer:'',
      forget:{
        captcha:'',
        email:'',
        newPassword:'',
        type:'',
        idCard:'',
        name:'',
        uuid:''
      }
    }
  },
  methods:{
    sendCapt(){
      getForgetCaptcha(this.forget).then(res=>{
      if(res.data.code==100) {
          this.forget.uuid=res.data.extend.uuid
          this.allowSend=false
          this.timer=setInterval(()=>{
            this.time--
            if(this.time<=0){
              clearInterval(this.timer)
              this.allowSend=true
              this.time=30
            }
          },1000)
        }
        this.$message.warning(res.data.msg)

      })
    },
    setNewPwd(){
      reSetPwd(this.forget).then(res=>{
        console.log(res)
        this.$message.success(res.data.msg)
        this.$router.push("/main/login")
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
#rePwd
  width 80%
  margin 80px auto
  box-shadow 0 0 2px grey
  height 500px
  padding 20px 50px
</style>
